package com.jeesite.modules.ic.web;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.Size;

import com.jeesite.modules.config.CardStatus;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.ic.entity.IcPackage;
import com.jeesite.modules.ic.service.IcPackageService;

/**
 * 套餐Controller
 * @author 李寒冰
 * @version 2024-07-16
 */
@Controller
@RequestMapping(value = "${adminPath}/ic/icPackage")
public class IcPackageController extends BaseController {

	@Autowired
	private IcPackageService icPackageService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public IcPackage get(String id, boolean isNewRecord) {
		return icPackageService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("ic:icPackage:view")
	@RequestMapping(value = {"list", ""})
	public String list(IcPackage icPackage, Model model) {
		model.addAttribute("icPackage", icPackage);
		return "modules/ic/icPackageList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("ic:icPackage:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<IcPackage> listData(IcPackage icPackage, HttpServletRequest request, HttpServletResponse response) {
		icPackage.setPage(new Page<>(request, response));
		Page<IcPackage> page = icPackageService.findPage(icPackage);
		return page;
	}

	public static void main(String[] args) {
		CardStatus[] cardStatuses = CardStatus.values();
		for (int i = 0; i < cardStatuses.length; i++) {
			System.out.println(cardStatuses[i].getNum()+cardStatuses[i].getText());
		}
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("ic:icPackage:view")
	@RequestMapping(value = "form")
	public String form(IcPackage icPackage, Model model) {
		model.addAttribute("icPackage", icPackage);
		return "modules/ic/icPackageForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("ic:icPackage:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated IcPackage icPackage) {
		icPackageService.save(icPackage);
		return renderResult(Global.TRUE, text("保存套餐成功！"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("ic:icPackage:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(IcPackage icPackage) {
		icPackageService.delete(icPackage);
		return renderResult(Global.TRUE, text("删除套餐成功！"));
	}

	/**
	 * 停用
	 */
	@RequiresPermissions("ic:icPackage:edit")
	@RequestMapping(value = "disable")
	@ResponseBody
	public String disable(IcPackage icPackage) {
		icPackage.setUseStatus(CardStatus.QIYONG_USE_NO.getNum());
		icPackageService.save(icPackage);
		return renderResult(Global.TRUE, text("停用套餐成功！"));
	}

	/**
	 * 启用
	 */
	@RequiresPermissions("ic:icPackage:edit")
	@RequestMapping(value = "enable")
	@ResponseBody
	public String enable(IcPackage icPackage) {
		Integer channalStatus = icPackage.getChannalStatus();
		String channalId = icPackage.getChannalId();
		if(channalId.equals(CardStatus.CHANNEL_ZEMI.getNum())){
			if(channalStatus==0){
				return renderResult(Global.FALSE, text("套餐已在渠道停用，不可启用！"));
			}
		}

		icPackage.setUseStatus(CardStatus.QIYONG_USE_YES.getNum());
		icPackageService.save(icPackage);
		return renderResult(Global.TRUE, text("启用套餐成功！"));
	}
	
}